package com.server.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.service.IAttachmentService;
import com.server.service.IForeignStudyService;
import com.server.web.DataVector;
import com.server.web.RequestInput;
import com.sun.accessibility.internal.resources.accessibility;
@Service("ForeignStudyService")
public class ForeignStudyService implements IForeignStudyService{
	
	private String tableName = "";
	@Autowired
	private CommonDataService commonDataService;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IAttachmentService attachmentService;
	
	@Override
	public DataVector<Map<String, Object>> getList(Integer type,String whereStr,
			String sortStr, RequestInput requestInput) {
		switch(type){
			case 1:
				tableName = "education.sicfa_con_reg_sit";
				break;
			case 2:
				tableName = "education.sicfa_for_ins_per";
				break;
			case 3:
				tableName = "education.sicfa_emr_hand";
				break;
			case 4:
				tableName = "education.sicfa_rel_sub";
				break;
			default:
				tableName = "education.sicfa_con_reg_sit";
				break;
		}
		return commonDataService.getListData(tableName, whereStr, sortStr, requestInput);
	}

	@Override
	public Map<String, Object> foreignDetail(Integer id,Integer type) {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<Map<String, Object>> attaList = new ArrayList<Map<String,Object>>();
		switch(type){
			case 1:
				tableName = "education.sicfa_con_reg_sit";
				break;
			case 2:
				tableName = "education.sicfa_for_ins_per";
				break;
			case 3:
				tableName = "education.sicfa_emr_hand";
				break;
			case 4:
				tableName = "education.sicfa_rel_sub";
				break;
			default:
				tableName = "education.sicfa_con_reg_sit";
				break;
		}
		String sql = "select * from %s where id=%d";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, tableName,id));
		if(list.size()>0){
			
			returnMap = list.get(0);
			if(returnMap.get("class")!=null){
				returnMap.put("class1", returnMap.get("class"));
			}
			if (type==4) {
				attaList = attachmentService.getList("foreign", id.toString());
				returnMap.put("atta", attaList);
			}
		}
		
		return returnMap;
	}

}
